﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class AppSettingDA : IAppSettingDA
    {
        private string APP_SETTING_ID = "@APP_SETTING_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string IS_MODIFIABLE = "@IS_MODIFIABLE";
        private string SQL_CHECK_APP_SETTING_ID_UNIQUE = " SELECT COUNT(1) FROM SYSADM.APP_SETTING WHERE APP_SETTING_ID = @APP_SETTING_ID ";
        private string SQL_INSERT_APP_SETTING = " INSERT INTO SYSADM.APP_SETTING ( APP_SETTING_ID, DESCR, VALUE, IS_MODIFIABLE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @APP_SETTING_ID, @DESCR, @VALUE, @IS_MODIFIABLE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_APP_SETTING = " SELECT  ROWID, APP_SETTING_ID, DESCR, VALUE, IS_MODIFIABLE, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  SYSADM.APP_SETTING ";
        private string SQL_UPDATE_APP_SETTING = " UPDATE SYSADM.APP_SETTING SET DESCR = @DESCR, VALUE = @VALUE, IS_MODIFIABLE = @IS_MODIFIABLE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE APP_SETTING_ID = @APP_SETTING_ID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string VALUE = "@VALUE";

        public bool CheckAppSettingIDUnique(string appSettingID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@APP_SETTING_ID", appSettingID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_APP_SETTING_ID_UNIQUE, paras).ToString() == "1");
        }

        public bool CheckAppSettingIDUnique(DataBase dataBase, DbTransaction tran, string appSettingID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@APP_SETTING_ID", appSettingID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_APP_SETTING_ID_UNIQUE, paras).ToString() == "1");
        }

        public List<AppSettingInfo> GetAllAppSetting()
        {
            List<AppSettingInfo> appSettingInfos = new List<AppSettingInfo>();
            AppSettingInfo appSettingInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, this.SQL_SELECT_ALL_APP_SETTING))
            {
                while (reader.Read())
                {
                    DateTime CS00001;
                    appSettingInfo = new AppSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["APP_SETTING_ID"].ToString(), reader["DESCR"].ToString(), reader["VALUE"].ToString(), reader["IS_MODIFIABLE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00001 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00001 = new DateTime()));
                    appSettingInfos.Add(appSettingInfo);
                }
            }
            return appSettingInfos;
        }

        public List<AppSettingInfo> GetAllAppSetting(DataBase dataBase, DbTransaction tran)
        {
            List<AppSettingInfo> appSettingInfos = new List<AppSettingInfo>();
            AppSettingInfo appSettingInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, this.SQL_SELECT_ALL_APP_SETTING);
            while (reader.Read())
            {
                DateTime CS00001;
                appSettingInfo = new AppSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["APP_SETTING_ID"].ToString(), reader["DESCR"].ToString(), reader["VALUE"].ToString(), reader["IS_MODIFIABLE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00001 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00001 = new DateTime()));
                appSettingInfos.Add(appSettingInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return appSettingInfos;
        }

        public AppSettingInfo GetAppSettingByID(string appsettingID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_APP_SETTING.Replace("@_@", whLoginID) + " WHERE APP_SETTING_ID = @APP_SETTING_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@APP_SETTING_ID", appsettingID) };
            AppSettingInfo appSettingInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    appSettingInfo = new AppSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["APP_SETTING_ID"].ToString(), reader["DESCR"].ToString(), reader["VALUE"].ToString(), reader["IS_MODIFIABLE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return appSettingInfo;
        }

        public AppSettingInfo GetAppSettingByID(DataBase dataBase, DbTransaction tran, string appsettingID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_APP_SETTING.Replace("@_@", whLoginID) + " WHERE APP_SETTING_ID = @APP_SETTING_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@APP_SETTING_ID", appsettingID) };
            AppSettingInfo appSettingInfo = null;
            using (IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    appSettingInfo = new AppSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["APP_SETTING_ID"].ToString(), reader["DESCR"].ToString(), reader["VALUE"].ToString(), reader["IS_MODIFIABLE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return appSettingInfo;
        }

        public DataSet GetAppSettingByQueryList(List<string> sqlWhere, AppSettingQueryEntity appsettingQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_APP_SETTING.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!appsettingQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, appsettingQuery.CurrentPage, appsettingQuery.PageSize, appsettingQuery.SortField, appsettingQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetAppSettingByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, AppSettingQueryEntity appsettingQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_APP_SETTING.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, appsettingQuery.CurrentPage, appsettingQuery.PageSize, appsettingQuery.SortField, appsettingQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertAppSetting(AppSettingInfo appSettingInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_AppSetting_Parameters(appSettingInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_APP_SETTING, paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertAppSetting(DataBase dataBase, DbTransaction tran, AppSettingInfo appSettingInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_AppSetting_Parameters(appSettingInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_APP_SETTING, paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_AppSetting_Parameters(AppSettingInfo appSettingInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.APP_SETTING_ID, SqlDbType.VarChar, 30), new SqlParameter(this.DESCR, SqlDbType.VarChar, 200), new SqlParameter(this.VALUE, SqlDbType.VarChar, 200), new SqlParameter(this.IS_MODIFIABLE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            paramArray[0].Value = appSettingInfo.AppSettingID;
            if (!string.IsNullOrEmpty(appSettingInfo.Descr))
            {
                paramArray[1].Value = appSettingInfo.Descr;
            }
            else
            {
                paramArray[1].Value = DBNull.Value;
            }
            paramArray[2].Value = appSettingInfo.Value;
            paramArray[3].Value = appSettingInfo.IsModifiable;
            paramArray[4].Value = appSettingInfo.CreatedBy;
            paramArray[5].Value = appSettingInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateAppSetting(AppSettingInfo appSettingInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_AppSetting_Parameters(appSettingInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_APP_SETTING, paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateAppSetting(DataBase dataBase, DbTransaction tran, AppSettingInfo appSettingInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_AppSetting_Parameters(appSettingInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_APP_SETTING, paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

